#pragma once

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <unordered_map>
#include <mutex>
#include "util.hpp"
namespace ns_index
{
    // 正排结构
    struct DocInfo
    {
        std::string title;
        std::string content;
        std::string url;
        uint64_t doc_id;
    };
    // 倒排结构
    struct InvertedElem
    {
        uint64_t doc_id;
        std::string word;
        int weight;
    };
    // 倒排拉链
    typedef std::vector<InvertedElem> InvertedList;

    class Index
    {
    private:
        // 成员变量
        std::vector<DocInfo> forward_index;                           // 正排索引
        std::unordered_map<std::string, InvertedList> inverted_index; // 倒排索引
    private:
        Index() {}
        Index(const Index &) = delete;
        Index &operator=(const Index &) = delete;

        static Index *instance;
        static std::mutex mtx;

    public:
        ~Index() {}

    public:
        static Index *GetInstance()
        {
            if (nullptr == instance)
            {
                mtx.lock();
                if (nullptr == instance)
                {
                    instance = new Index();
                }
                mtx.unlock();
            }
            return instance;
        }
        // 根据doc_id查找文档
        DocInfo *GetForWardIndex(uint64_t doc_id)
        {
            if (doc_id > forward_index.size())
            {
                std::cerr << "doc_id error" << std::endl;
                return nullptr;
            }
            return &forward_index[doc_id];
        }
        // 根据关键词获得倒排拉链
        InvertedList *GetInvertedList(const std::string &word)
        {
            auto lter = inverted_index.find(word);
            if (lter == inverted_index.end())
            {
                std::cerr << "word error" << std::endl;
                return nullptr;
            }
            return &(lter->second);
        }
        // 构建索引
        bool BuildIndex(const std::string &input) // 获取处理完成的数据 一行由'\3'分隔的数据
        {
            // 打开文件
            std::ifstream in(input, std::ios::in | std::ios::binary);
            if (!in.is_open())
            {
                std::cerr << input << " open error" << std::endl;
                return false;
            }
            // Forword
            //  建立索引
            std::string line;
            while (std::getline(in, line))
            {
                // 建立正排索引
                DocInfo *doc = BuildForwardIndex(line);
                if (doc == nullptr)
                {
                    std::cerr << line << ": build error" << std::endl;
                    continue;
                }
                // 建立倒排索引
                BuileInvertedIndex(*doc);
            }
            return true;
        }

    private:
        // 构建正排索引
        DocInfo *BuildForwardIndex(const std::string &line)
        {
            std::vector<std::string> results;
            // 1.切割字符串
            const std::string sep = "\3";
            ns_util::StringUtil::Split(line, &results, sep);
            if (3 != results.size())
            {
                return nullptr;
            }
            // 2.将切割的各个部分填充到DocInof中
            DocInfo doc;
            doc.title = results[0];
            doc.content = results[1];
            doc.url = results[2];
            doc.doc_id = forward_index.size();
            // 3.将DocInfo插入vector
            forward_index.push_back(std::move(doc));
            return &forward_index.back();
        }
        // 构建倒排索引
        bool BuileInvertedIndex(const DocInfo &doc)
        {
            struct word_cut
            {
                int title_cnt;
                int content_cnt;

                word_cut()
                    : title_cnt(0), content_cnt(0) {}
            };
            // 存储词频
            std::unordered_map<std::string, word_cut> word_map;
            // 标题分词
            std::vector<std::string> title_words;
            ns_util::JiebaUtil::CutString(doc.title, &title_words);
            // 标题词频统计
            for (auto s : title_words)
            {
                boost::to_lower(s); // 转成小写
                word_map[s].title_cnt++;
            }
            // 内容分词
            std::vector<std::string> content_words;
            ns_util::JiebaUtil::CutString(doc.content, &content_words);
            // 内容词频统计
            for (auto s : content_words)
            {
                boost::to_lower(s); // 转成小写
                word_map[s].content_cnt++;
            }
#define X 10
#define Y 1
            for (auto &word_pair : word_map)
            {
                InvertedElem item;
                item.doc_id = doc.doc_id;
                item.word = word_pair.first;
                item.weight = X * word_pair.second.title_cnt + Y * word_pair.second.content_cnt;
                InvertedList &inverted_list = inverted_index[word_pair.first];
                inverted_list.push_back(std::move(item));
            }
            return true;
        }
    };
    std::mutex Index::mtx;
    Index *Index::instance = nullptr;

}
